﻿<!--#include file="../inc/common.asp" -->
<%
'将动网BBS的贴子转为Blog文章
'
'author: evan zhao
'

    call OpenDB
    
    response.AddHeader "Last-Modified", DateToGMT(Now())
    response.Expires = 0
    response.CacheControl = "no-cache"
  
    if not userObj.isAdmin then
        response.write "您没有权限"
        call closeDB()
        response.end
    end if    
    

    function toHTML(fString)
      if isnull(fString) then
          toHTML = ""
      else
          fString = Replace(fString, CHR(32), "&nbsp;")
          fString = Replace(fString, CHR(9), "&nbsp;")
          fString = Replace(fString, CHR(34), "&quot;")
          fString = Replace(fString, CHR(39), "&#39;")
          fString = Replace(fString, CHR(13), "")
          fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
          fString = Replace(fString, CHR(10), "<BR> ")
          fString = Replace(fString, " ", "&nbsp;")
          
          toHTML = fString
      end if
    end function
      
    if request("update")=" 转换 " then

        Server.ScriptTimeOut = 600000
    
        dim dvbbs_db_path
        dvbbs_db_path = request("dvbbs_db_path")
        
        if dvbbs_db_path="" then
            message("请输入动网论坛数据库路径")
        end if
        
        
    	dim conn2
    	dim connstr2
    	Set conn2 = Server.CreateObject("ADODB.Connection")
    	'建立动网论坛数据库连接
    	connstr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dvbbs_db_path)
    	
    	on error resume next
    	conn2.Open connstr2
    	
    	if Err.number<>0 then
            message(err.description)
        end if
        
        conn2.execute(" alter table bbs1 add blog_flag integer ")
                
        Err.clear
        
        on error goto 0
        
        dim   user_id, board_id, blog_id, count
        user_id = clng(request("user_id"))
        board_id = clng(request("board_id"))
        blog_id = clng(request("blog_id"))
        count =  clng(request("count"))
    
        dim title,detail, user_name, insert_time, post_id, i, j, rs2, rs3, rs4
        
        set rs2 = Server.CreateObject("ADODB.Recordset")
        set rs3 = Server.CreateObject("ADODB.Recordset")
        set rs4 = Server.CreateObject("ADODB.Recordset")
    
        sql = " select top " & count & " AnnounceID,  Topic, Body, DateAndTime, userId, UserName, blog_flag from bbs1 where  userid="&user_id&" and parentid=0 and blog_flag is null and boardid=" & board_id
        
        response.write sql
        'response.end
        rs.open sql, conn2, 1, 3

        sql = " select content_id, blog_id, title, detail, user_id, user_name, insert_time, start_date, end_date, post_id ,reply_count from blog_content where content_id=0 "
        rs2.open sql, conn, 1, 3

        sql = " select comment_id, blog_id, content_id, detail, user_id, user_name, insert_time from blog_comments where comment_id=0"
        rs4.open sql, conn, 1, 3

        i=0
        while not rs.eof
            i=i+1
            title = rs("Topic")
            'response.write "<br>"&title&"<br>"
            detail = toHTML(rs("Body"))
            user_id = rs("userid")
            user_name = rs("username")
            insert_time = rs("DateAndTime")
            post_id= rs("announceid")

            'sql = " insert into blog_content(blog_id, title, detail, user_id, user_name, insert_time, start_date, end_date, post_id ) values " &_
            '          " (" & blog_id & ", '" & title &"', '" & detail &"', " & user_Id & ", '" & user_Name &"', '" & insert_time &"', '" & insert_time &"', '" & insert_time &"', " & post_id &"  )"
            'conn.execute(sql)

            rs2.addNew()
            rs2("blog_id") = blog_id
            rs2("title") = title
            rs2("detail") = detail
            rs2("user_id") = user_id
            rs2("user_name") = user_name
            rs2("insert_time") = insert_time
            rs2("start_date") = insert_time
            rs2("end_date") = insert_time
            rs2("post_id") = post_id
            rs2.update
            
            sql = " select AnnounceID,  Topic, Body, DateAndTime, userId, UserName, blog_flag from bbs1 where  parentid<>0 and blog_flag is null and rootid=" & post_id
            rs3.open sql, conn2, 1, 1
            j=0
            while not rs3.eof
                j=j+1
                rs4.addNew()
                rs4("blog_id") = blog_id
                rs4("content_id") =  rs2("content_id")
                rs4("detail") =  toHTML(rs3("Body"))
                rs4("user_id") = rs3("userid")
                rs4("user_name") = rs3("UserName")
                rs4("insert_time") = rs3("DateAndTime")
                rs4.update()
                
                rs3.moveNext()
            wend
            if j>0 then
                rs2("reply_count")=j
                rs2.update()
            end if
            
            rs3.close()

            rs("blog_flag")=1
            rs.update()
            rs.moveNext()
        wend
        
        set rs3=nothing

        rs4.close()
        set rs4=nothing

        rs2.close()
        set rs2=nothing
        

        conn2.close()

        response.write("<p>DONE:" &i&" </p>")    
        
    end if

    if userDB<>blogDB then    
%>
在转换动网贴子之前请确认已经将动网数据库中[user]表数据导入blog数据库的[user]表中
<%
    end if
%>
<script language=javascript>
function check(){
    if(form1.dvbbs_db_path.value==""){
        alert("请输入dvbbs_db_path");
        return false;
    }
    if(form1.board_id.value==""){
        alert("请输入board_id");
        return false;
    }    
    if(form1.user_id.value==""){
        alert("请输入user_id");
        return false;
    }
    if(form1.blog_id.value==""){
        alert("请输入blog_id");
        return false;
    }    
    if(form1.count.value==""){
        alert("请输入处理条数");
        return false;
    }        
    return true;
}
</script>
<font color=red>目前只支持dvbbs 5.x和6.x</font>
<form name=form1 method=post onsubmit="return check()">
<input type=hidden name=start value=true><br>
dvbbs_db_path:<input type=text name=dvbbs_db_path value="<%=request("dvbbs_db_path")%>" >(例如：../../bbs/data/dvbbs.mdb)<br>
board_id:<input type=text name=board_id value="<%=request("board_id")%>" >导出该板块的贴子<br>
user_id:<input type=text name=user_id value="<%=request("user_id")%>" >导出该用户的贴子<br>
blog_id:<input type=text name=blog_id value="<%=request("blog_id")%>" >导入Blog的ID<br>
处理条数:<input type=text name=count value="<%=request("count")%>" >建议<100，可以反复执行多次直到全部转换完毕<br>
<input type=submit name="update" value=" 转换 " >
<p><a href="index.asp">返回</a></p>
</form>



<!--#include file="../inc/end.asp" -->